<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 160px"
        class="filter-item"
        placeholder="请输入讲解器编号"
        v-model="listQuery.device_number"
      ></el-input>

      <el-button class="filter-item" size="mini" plain @click="handleReset">重置</el-button>
      <el-button class="filter-item" size="mini" type="primary" @click="handleFilter"
        >查询</el-button
      >
    </div>
    <Table :data="list" :columns="columns" :loading="listLoading">
      <template v-slot:cabinet_number>
        <el-table-column label="机柜编号" width="100" align="center">
          <template slot-scope="scope">
            <div class="battery">
              {{ scope.row.cabinet ? scope.row.cabinet.cabinet_number : '-' }}
            </div>
          </template>
        </el-table-column>
      </template>
      <template v-slot:remaining_battery>
        <el-table-column label="剩余电量" width="100" align="center">
          <template slot-scope="scope">
            <div class="battery">
              <img
                class="battery_icon"
                v-if="scope.row.remaining_battery > 20"
                src="@/assets/battery_icon1.png"
              />
              <img class="battery_icon" v-else src="@/assets/battery_icon2.png" />
              {{ scope.row.remaining_battery }}%
            </div>
          </template>
        </el-table-column>
      </template>
      <template v-slot:position>
        <el-table-column label="仓口号" width="100" align="center">
          <template slot-scope="scope">
            <div class="battery">{{ scope.row.cabinet ? scope.row.cabinet.position : '-' }}</div>
          </template>
        </el-table-column>
      </template>
      <template v-slot:cabinet_status>
        <el-table-column label="机柜状态" width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.cabinet_status === 'inside' ? '机柜中' : '机柜外' }}
          </template>
        </el-table-column>
      </template>

      <template v-slot:action>
        <el-table-column label="操作" minWidth="100" fixed="right">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              :content="!scope.row.is_locked ? '锁定' : '解锁'"
              placement="top"
              v-if="scope.row.cabinet"
            >
              <i
                v-if="!scope.row.is_locked && scope.row.cabinet"
                class="el-icon-unlock"
                style="cursor: pointer"
                @click="handleLock(scope.row, 'lock')"
              ></i>
              <i
                v-else
                class="el-icon-lock"
                style="cursor: pointer; color: #f00"
                @click="handleLock(scope.row, 'unlock')"
              ></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </template>
    </Table>
    <div class="pagination-container">
      <pagination
        :total="count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.size"
        @pagination="fetchData"
      />
    </div>
  </div>
</template>

<script>
import { getList, deviceLock } from '@/api/device'
export default {
  name: 'DeviceIndex',
  data() {
    return {
      list: null,
      count: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        size: 10,
        device_number: ''
      },
      columns: [
        {
          type: 'index',
          align: 'center',
          label: '序号',
          width: '50'
        },
        {
          label: '讲解器编号',
          width: '160',
          prop: 'device_number',
          align: 'center'
        },
        {
          label: '讲解器版本',
          width: '160',
          prop: 'device_version',
          align: 'center'
        },
        {
          label: '机柜编号',
          slot: 'cabinet_number'
        },

        {
          label: '当前电量',
          slot: 'remaining_battery'
        },
        {
          label: '仓口号',
          slot: 'position'
        },
        {
          label: '机柜状态',
          slot: 'cabinet_status'
        },

        {
          label: '操作',
          slot: 'action'
        }
      ]
    }
  },

  created() {
    this.fetchData()
  },
  mounted() {},
  methods: {
    // 获取数据
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then((res) => {
        this.list = res.data.list_data
        this.count = res.data.paging.count
        this.listLoading = false
      })
    },

    // 筛选
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    /**
     * @description:
     * @return {*}
     */
    handleReset() {
      this.listQuery = {
        page: 1,
        size: 10,
        device_number: ''
      }
      this.fetchData()
    },
    handleLock(item, type) {
      if (!item.cabinet) return this.$message.error('请先绑定机柜')
      const temp = {
        cabinet_id: item.cabinet.cabinet_number,
        device_numbers: [item.device_number],
        lock_status: type
      }
      this.$confirm(`确认${type === 'lock' ? '锁定' : '解锁'}该项目吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deviceLock(temp).then(() => {
            this.$notify({
              title: '成功',
              message: `${type === 'lock' ? '锁定' : '解锁'}成功`,
              type: 'success'
            })
            this.fetchData()
          })
        })
        .catch(() => {})
    }
  }
}
</script>
<style scoped>
.filter-container {
  justify-content: flex-start !important;
  flex-wrap: wrap;
}
.el-button {
  margin-bottom: 5px;
  margin-left: 0;
  margin-right: 5px;
}
.battery {
  display: flex;
  align-items: center;
  justify-content: center;
}
.battery_icon {
  width: 20px;
  margin-right: 4px;
}
</style>
